package com.cxz.study.domain.util;

import java.lang.reflect.InvocationTargetException;

/**
 * @className: Asserts
 * @author: ocxz1
 * @date: 2025/6/13 下午7:19
 * @Version: 1.0
 * @description: ddd-struct3
 */
public class Asserts {

    public static <T extends RuntimeException> void isTrue(boolean expression, Class<T> exceptionClass) {
        if (!expression) {
            try {
                throw exceptionClass.getDeclaredConstructor().newInstance();
            } catch (InvocationTargetException | InstantiationException | IllegalAccessException |
                     NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static <T extends RuntimeException> void isTrue(boolean expression, T exception) {
        if (!expression) {
            throw exception;
        }
    }

    public static <T extends RuntimeException> void isFalse(boolean expression, Class<T> exceptionClass) {
        isTrue(!expression, exceptionClass);
    }

    public static <T extends RuntimeException> void isFalse(boolean expression, T exception) {
        isTrue(!expression, exception);
    }
}
